package edu.umd.ion.wavemeter.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;

import edu.umd.ion.wavemeter.service.WavemeterData;
import edu.umd.ion.wavemeter.service.WavemeterServiceAsync;

public class ControlWidget extends Composite implements NeedsDataPoll {
	public static final String[] OperationStates =
	{ "Stopped", "Adjusting", "Measuring" };
	
	private WavemeterServiceAsync WavemeterService;
	private CheckBox WideMode, FastMode, PulsedMode, SwitcherMode, RegulationEnable;
	private Label OperationStateLabel;
	private HTML ErrorMessage = new HTML();
	
	private AsyncCallback<Void> CommandCallback = new AsyncCallback<Void>() {
		public void onFailure(Throwable caught) {
			try {
				ErrorMessage.setHTML("Command: " + caught.getMessage());
				throw caught;
			} catch (IncompatibleRemoteServiceException e) {
				// this client is not compatible with the server; cleanup and refresh the 
				// browser
				reload();
			} catch (Throwable e) {
				// last resort -- a very unexpected exception
			}
		}

		public void onSuccess(Void result) {
			ErrorMessage.setHTML("");
		}
	};

	private native void reload() /*-{
    $wnd.location.reload();
   }-*/;

	
	public void set(WavemeterData data)
	{
		WideMode.setValue(data.WideMode);
		FastMode.setValue(data.FastMode);
		PulsedMode.setValue(data.PulseMode);
		SwitcherMode.setValue(data.SwitcherMode);
		RegulationEnable.setValue(data.RegulationEnable);
		if (data.Operation>2 || data.Operation<0)
			OperationStateLabel.setText("Unexpected Value");
		else
			OperationStateLabel.setText( OperationStates[data.Operation] );
	}
	
	public ControlWidget(WavemeterServiceAsync cb, Panel ErrorPanel) {
		WavemeterService = cb;
		
		ErrorPanel.add( ErrorMessage );
		HorizontalPanel StartStop = new HorizontalPanel();
		OperationStateLabel = new Label();
		
		Button StartButton = new Button("Start", new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.setMeasurement(true,CommandCallback); }
		});
		Button StopButton = new Button("Stop", new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.setMeasurement(false,CommandCallback); }
		});
		Button ShutdownButton = new Button("Shutdown Server Process",  new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.shutdown(CommandCallback); }
		});
		StartStop.add(OperationStateLabel);
		StartStop.add(StartButton);
		StartStop.add(StopButton);
		StartStop.add(ShutdownButton);
		
		VerticalPanel Container = new VerticalPanel();
		Container.add(StartStop); 

		WideMode = new CheckBox("Wide mode");
		WideMode.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.setWideMode(WideMode.getValue(),CommandCallback); }
		});
		Container.add(WideMode);
		
		FastMode = new CheckBox("Fast mode");
		FastMode.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.setFastMode(FastMode.getValue(),CommandCallback); }
		});
		Container.add(FastMode);
				
		PulsedMode = new CheckBox("Pulsed mode");
		PulsedMode.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.setPulseMode(PulsedMode.getValue(),CommandCallback); }
		});
		Container.add(PulsedMode);
				
		SwitcherMode = new CheckBox("Switcher mode");
		SwitcherMode.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) { WavemeterService.setSwitcherMode(SwitcherMode.getValue(),CommandCallback); }
		});
		Container.add(SwitcherMode);
		
		RegulationEnable = new CheckBox("Enable Regulation");
		RegulationEnable.setEnabled(false); // The command does not work, has to be done on the WLU Program itself
		//RegulationEnable.addClickListener(new ClickListener(){
		//	public void onClick(Widget sender) { callback.setRegulationEnable(((CheckBox) sender).isChecked()); }
		//});
		Container.add(RegulationEnable);
		
		initWidget(Container);
	}
}
